//
//  ByViewRegistrationSubjectHeader.m
//  jinpuyiyuan_ios
//
//  Created by pro on 2017/12/11.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "ByViewRegistrationSubjectHeader.h"

#import "ByCellRegistrationSegment.h"

@interface ByViewRegistrationSubjectHeader()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) NSMutableArray *timeAry;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, assign) NSInteger selectIndex;            // 记录选中的cell
@end

@implementation ByViewRegistrationSubjectHeader
- (instancetype)init{
    if (self = [super init]) {
        [self createViews];
        [self createDatas];
        _selectIndex = 0;
    }
    return self;
}

- (void)createViews{
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80) collectionViewLayout:self.layout];
    [self addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[ByCellRegistrationSegment class] forCellWithReuseIdentifier:@"segmentCell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    UIView *line = [[UIView alloc] init];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.bottom.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 1));
    }];
    line.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];

}

#pragma mark - 构造数据
- (void)createDatas{
    for (int i = 0; i < 30; i ++) {
        NSDate *date = [NSDate dateWithDaysFromNow:i];
        NSString *week = [NSDate getDateWeekDayForStringFormatWithDate:date];
        NSDictionary *dic = @{@"week":week,@"date":date};
        [self.timeAry addObject:dic];
    }
    [self.collectionView reloadData];
}


#pragma mark - getter
- (NSMutableArray *)timeAry{
    if (!_timeAry) {
        _timeAry = [NSMutableArray array];
    }
    return _timeAry;
}

- (UICollectionViewFlowLayout *)layout{
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _layout.itemSize = CGSizeMake(SCREEN_WIDTH / 7, 80);
        _layout.minimumLineSpacing = 0;
        _layout.minimumInteritemSpacing = 0;
        _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }
    return _layout;
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.timeAry.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ByCellRegistrationSegment *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"segmentCell" forIndexPath:indexPath];
    cell.dic = self.timeAry[indexPath.item];
    if (indexPath.item == self.selectIndex) {
        cell.selected = YES;
    }else{
        cell.selected = NO;
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.selectIndex) {
        return;
    }
    ByCellRegistrationSegment *cell = (ByCellRegistrationSegment *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.selected = YES;
    
    NSIndexPath *preIndexPath = [NSIndexPath indexPathForItem:self.selectIndex inSection:0];
    ByCellRegistrationSegment *pre_cell = (ByCellRegistrationSegment *)[collectionView cellForItemAtIndexPath:preIndexPath];
    pre_cell.selected = NO;
    if (self.selectedBlock) {
        self.selectedBlock(self.timeAry[indexPath.item]);
    }
}


@end
